<?php
// includes for the TCPDF library
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
setlocale(LC_MONETARY, 'en_US');

// PHP Settings to display errors in the script.
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Get the ID of the proposal from the URL
$id = $_GET['id'];

require_once('proposal_query.php');

// Define the text for the terms and conditions of the report.

// Authorization Text
$auth_line1  = 'It is the responsibility of the Client / Agency to inform Third Party Adservers of our Creative Specifications and adhere to the 3 working days lead time';
$auth_line2  = '* All orders are valid for 72 hours. By signing this Digital Advertising Agreement you acknowledge the Display Advertising Terms and Conditions contained on our website or attached with the Digital Advertising Agreement.';
$auth_line3  = 'APN Online (Australia) Pty Limited';
$auth_line4  = 'Telephone: +61 7 3327 3333 Fax: +61 7 3368 4991';
$auth_line5  = 'PO Box 1578 MILTON QLD 4064';
$auth_line6  = 'ABN - 93 119 243 944';



// Extend the PDF class 
class MYPDF extends TCPDF {
// *****************************************************
//
// Header Definition
//    Place the header graphic
//    Display the company address and Document Title 
//
// *****************************************************
	public function Header() {
		// Set the image quality
		$this->setJPEGQuality(90);
		// Place the image on the page at x=10, y=10, width=50. The null string is a placeholder for a hyperlink.
		$this->Image('tcpdf/images/APNOnlineSML.JPG', 178, 10, 20, 0, 'JPG', '');
	}
// *****************************************************
//
// Footer Definition
//    Place the footer information
//
// *****************************************************
	public function Footer() {
		$this->SetY(-15);
		$this->SetFont(PDF_FONT_NAME_MAIN, '', 8);
		$this->SetTextColor(200);
		$this->Cell(0, 10, '', 0, false, 'C');
	}
// *****************************************************
//
// End of Header and Footer Definitions
//
// *****************************************************

// This is a text area with no box around the text.	
	public function CreateTextArea($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+10, $y); // 10 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(0,0,20);
		$this->Cell($width, $height, $textval, 0, false, $align);
	}
// This is a text area WITH a box around the text.
	public function CreateTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+12, $y); // 12 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(0,0,20);
		//$w,$h,$txt,$border = 0,$align = 'J',$fill = false,$ln = 1,$x = '',$y = '',$reseth = true,$stretch = 0,$ishtml = false,$autopadding = true,$maxh = 0,$valign = 'T',$fitcell = false
		$this->MultiCell($width, $height, $textval, 0, $align, false , 0,'','', true,0,false,true,6,'M',true);
	}
// This is a purple shaded a text area WITH a box around the text.
	public function CreateHeaderTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+10, $y); // 10 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(255,255,255);
		$this->SetFillColor(51,51,153); // set the fill color as light gray
		$this->Cell($width, $height, $textval, 1, false, $align, true);
	}
// This is a gray shaded a text area WITH a box around the text.
	public function CreateTotalTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+10, $y); // 10 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(255,255,255);
		$this->SetFillColor(200); // set the fill color as light gray
		$this->Cell($width, $height, $textval, 1, false, $align, true);
	}
// This is a shaded a text area WITHOUT a box around the text.
	public function CreateHeaderArea($textval, $x = 0, $y, $width = 0, $height = 8, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+12, $y); // 12 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(0,0,20);
		$this->SetFillColor(200); // set the fill color as light gray
//		$this->Cell($width, $height, $textval, 0, false, $align, true,'',0,false,'','B');
		$this->MultiCell($width, $height, $textval, 0, $align, true , 0,'','', true,0,false,true,6,'B',true);
	}
// This is a shaded text box WITH a box around the text used for the expanding monthly revenue totals at the bottom of the proposal	
	public function CreateShadedTextBox($textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L') {
		$this->SetXY($x+10, $y); // 10 = margin left
		$this->SetFont(PDF_FONT_NAME_MAIN, $fontstyle, $fontsize);
		$this->SetTextColor(0,0,20);
		$this->SetFillColor(200); // set the fill color as light gray
		//$w,$h,$txt,$border = 0,$align = 'J',$fill = false,$ln = 1,$x = '',$y = '',$reseth = true,$stretch = 0,$ishtml = false,$autopadding = true,$maxh = 0,$valign = 'T',$fitcell = false
		$this->MultiCell($width, $height, $textval, 1, $align, true , 0,'','', true,0,false,true,10,'M',true);
//		$this->Cell($width, $height, $textval, 1, false, $align, true);
	}
}


// *****************************************************
//
// create a PDF object
//    Define the author, title, subject and kewords of the PDF file
//    Create the Group Header information 
//
// *****************************************************
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 
// set document (meta) information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('aiMatch, Inc.');
$pdf->SetTitle('Advertising Proposal');
$pdf->SetSubject('Advertising Proposal');
$pdf->SetKeywords('Proposal, Advertising, Agency');
 
// add a page
$pdf->AddPage();
 
// create proposal information box
// Top of Page
// $textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L'
$pdf->CreateTextArea('ADVERTISING AGREEMENT', 2, 29.5, 186, 8, 6, 'B','C');
//-----------
$pdf->CreateHeaderTextBox('CAMPAIGN DETAILS', 2, 35, 186, 3, 6, 'B', 'C');
//-----------
$pdf->CreateTextArea('Agreement Number:', 0, 37, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($id, 37, 37, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Order Date:', 80, 37, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(date('d F Y',time()), 107, 37, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Campaign Name:', 0, 39.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ProposalName, 37, 39.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Type of Campaign:', 80, 39.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 39.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Start Date:', 0, 42, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ProposalStartDate, 37, 42, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('End Date:', 80, 42, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($ProposalEndDate, 107, 42, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Agency:', 80, 44.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 107, 44.5, 25, 8, 6, 'B','L');
$pdf->CreateTextArea('Rebate:', 120, 44.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 147, 44.5, 25, 8, 6,'B','L');
$pdf->CreateTextArea('GST Exempt:', 80, 47, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 107, 47, 25, 8, 6,'B','L');
//------------
$pdf->CreateHeaderTextBox('AGENCY / CLIENT INFORMATION', 2, 55, 186, 3, 6, 'B', 'C');
//-----------
$pdf->CreateTextArea('Client Name:', 0, 57, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($AdvertiserName, 37, 57, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Agency Name:', 80, 57, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyName, 107, 57, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('New Client:', 0, 59.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 59.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('', 80, 59.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 59.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Internal Client Number:', 0, 62, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 62, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('', 80, 62, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 62, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Address:', 0, 64.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientAddress1, 37, 64.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Address:', 80, 64.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyAddress1, 107, 64.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('', 0, 67, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientCity.", ".$ClientState." ".$ClientZip, 37, 67, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('', 80, 67, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyCity.", ".$AgencyState." ".$AgencyZip, 107, 67, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Phone Number:', 0, 69.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientPhone, 37, 69.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Phone Number:', 80, 69.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyPhone, 107, 69.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Fax Number:', 0, 72, 35, 8, 6, 'B','R');
$pdf->CreateTextArea('', 37, 72, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Fax Number:', 80, 72, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 72, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Email Address:', 0, 74.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientEmail, 37, 74.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Email Address:', 80, 74.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyEmail, 107, 74.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Contact Name:', 0, 77, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientFullName, 37, 77, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Contact Name:', 80, 77, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyContact, 107, 77, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Position:', 0, 79.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($ClientTitle, 37, 79.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Position:', 80, 79.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($AgencyTitle, 107, 79.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateHeaderTextBox('BILLING INFORMATION', 2, 87, 186, 3, 6, 'B', 'C');
//------------
$pdf->CreateTextArea('PO Number Required:', 0, 89, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 89, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('', 80, 89, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 89, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Billing Company:', 0, 91.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToCompanyName, 37, 91.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Phone Number:', 80, 91.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToPhone, 107, 91.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Address:', 0, 94, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToAddress, 37, 94, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Fax Number:', 80, 94, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 107, 94, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('', 0, 96.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToCity." ".$BillToState." ".$BillToZip." ", 37, 96.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Contact Name:', 80, 96.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToFullName, 107, 96.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Email Address:', 0, 99, 35, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToEmail, 37, 99, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Position:', 80, 99, 25, 8, 6, 'B','R');
$pdf->CreateTextArea($BillToTitle, 107, 99, 25, 8, 6,'B','L');
//------------
$pdf->CreateHeaderTextBox('CREATIVE / TRAFFIC INFORMATION', 2, 106.5, 186, 3, 6, 'B', 'C');
//------------
$pdf->CreateTextArea('Creative Company:', 0, 108.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 108.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Phone Number:', 80, 108.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 107, 108.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Contact Name:', 0, 111, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 111, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('', 80, 111, 25, 8, 6, 'B','R');
$pdf->CreateTextArea('', 107, 111, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Email Address:', 0, 113.5, 35, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 37, 113.5, 50, 8, 6, 'B','L');
$pdf->CreateTextArea('Special Creative Instructions:', 80, 113.5, 25, 8, 6, 'B','R');
$pdf->CreateTextArea(' ', 107, 113.5, 25, 8, 6,'B','L');
//------------
$pdf->CreateTextArea('Media*', 1, 117, 35, 8, 6, 'B','L');

// *****************************************************
//
// Start of the Group Header creation definitions.
//
// *****************************************************
// list headers - the line width is 185.
// $textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L'

$pdf->CreateHeaderArea('Start Date', 0, 125, 15, 6, 5, '', 'C');
$pdf->CreateHeaderArea('End Date', 15, 125, 15, 6, 5, '', 'C');
$pdf->CreateHeaderArea('Locations', 30, 125, 79, 6, 5, '', 'C');
$pdf->CreateHeaderArea('Flight Name', 109, 125, 25, 6, 5, '', 'C');
$pdf->CreateHeaderArea('Planned Impressions', 134, 125, 15, 6, 5, '', 'C');
$pdf->CreateHeaderArea('Clicks', 149, 125, 7, 6, 5, '', 'C');
$pdf->CreateHeaderArea('CPM', 156, 125, 8, 6, 5, '', 'C');
$pdf->CreateHeaderArea('CPC', 164, 125, 8, 6, 5, '', 'C');
$pdf->CreateHeaderArea('Total Campaign Cost (ex GST)', 172, 125, 15, 6, 5, '', 'C');
$pdf->line(12,131,199,131);

// *****************************************************
//
// End of the Group Header creation definitions.
//
// *****************************************************
 
$currY = 131;

$UnRoundedTotal=0;
// Cycle through the mysql result set and put the values into variables
$i=0;
while ($i < $line_item_num) {
/*	$StartDate[$i] = date('d M Y',$LineItemStartDate[$i]);
	if($StartDate == '31 Dec 1969'){
		$StartDate = '';}
	$EndDate[$i] = date('d M Y',$LineItemEndDate[$i]);
	if($EndDate == '31 Dec 1969'){
		$EndDate = '';}
*/	
	// Calculate the number of days and create the NoUnits portion of the PDF
	if($LineItemFlatRate[$i]){
		$AgreedPrice[$i] = $LineItemFlatRate[$i];
		$UnRoundedAgreedPrice[$i] = $LineItemFlatRate[$i];
		$AgreedPrice[$i] = $AgreedPrice[$i] + $LineItemAdditionalFees[$i];
		$UnRoundedAgreedPrice[$i] = $UnRoundedAgreedPrice[$i] + $LineItemAdditionalFees[$i];
		if($LineItemEndDate[$i]=='none') {
			$EndDate = $LineItemStartDate[$i];
		}
		else {
			$EndDate = $LineItemEndDate[$i]; 
		}
		$date1 = date_create($LineItemStartDate[$i]);
		$date2 = date_create($EndDate);
		$date3 = date_diff($date1, $date2);
		$days = $date3->format('%a')+1;
		$NoUnits = $days." Days";
	}
	else if ($LineItemImpressions[$i]){
		$AgreedPrice[$i] = round((($LineItemImpressions[$i]/1000)*$LineItemCostImpressions[$i]),2);
		$UnRoundedAgreedPrice[$i] = (($LineItemImpressions[$i]/1000)*$LineItemCostImpressions[$i]);
		$AgreedPrice[$i] = $AgreedPrice[$i] + $LineItemAdditionalFees[$i];
		$UnRoundedAgreedPrice[$i] = $UnRoundedAgreedPrice[$i] + $LineItemAdditionalFees[$i];
		if($AgreedPrice[$i] < 0){
			$AgreedPrice[$i] = 0; }
		$NoUnits[$i] = number_format($LineItemImpressions[$i])." Impressions";
		$LineItemImpressions[$i] = number_format($LineItemImpressions[$i]);
		if(!$LineItemClicks[$i]){
			$LineItemClicks[$i] = " - "; }
		if(!$LineItemCostClicks[$i]) {
			$LineItemCostClicks[$i] = " - "; }
	}
	else if ($LineItemClicks[$i]){
		$AgreedPrice[$i] = round(($LineItemClicks[$i]*$LineItemCostClicks[$i]),2);
		$UnRoundedAgreedPrice[$i] = ($LineItemClicks[$i]*$LineItemCostClicks[$i]);
		$AgreedPrice[$i] = $AgreedPrice[$i] + $LineItemAdditionalFees[$i];
		$UnRoundedAgreedPrice[$i] = $UnRoundedAgreedPrice[$i] + $LineItemAdditionalFees[$i];
		if($AgreedPrice[$i] < 0){
			$AgreedPrice[$i] = 0; }
		$NoUnits[$i] = number_format($LineItemClicks[$i])." Clicks";
		$LineItemClicks[$i] = number_format($LineItemClicks[$i]);
		$CPC[$i] = number_format($AgreedPrice[$i]/($LineItemClicks[$i]), 2, '.', ',');
		if(!$LineItemImpressions[$i]){
			$LineItemImpressions[$i] = " - "; }
		if(!$LineItemCostImpressions[$i]) {
			$LineItemCostImpressions[$i] = " - "; }
		
	}
	else if ($LineItemActions[$i]){
		$AgreedPrice[$i] = round(($LineItemActions*$LineItemCostActions[$i]),2);
		$UnRoundedAgreedPrice[$i] = ($LineItemActions*$LineItemCostActions[$i]);
		$AgreedPrice[$i] = $AgreedPrice[$i] + $LineItemAdditionalFees[$i];
		$UnRoundedAgreedPrice[$i] = $UnRoundedAgreedPrice[$i] + $LineItemAdditionalFees[$i];
		if($AgreedPrice[$i] < 0){
			$AgreedPrice[$i] = 0; }
		$NoUnits[$i] = number_format($LineItemActions[$i])." Actions";
	}
	else {
		$AgreedPrice[$i] = 0;
	}
// *****************************************************
//
// Create each line item for the Booking Details on the proposal.
//    Format the float for the dollars and cents
//    Keep a running total for the end of the report.
//
// *****************************************************

$StartDate = date('d M Y',strtotime($LineItemStartDateTZ[$i]));
$pdf->CreateTextBox($StartDate, 0, $currY, 15, 6, 5, '', 'C');
if($LineItemEndDateTZ[$i] != 'none') {
	$EndDate = date('d M Y',strtotime($LineItemEndDate[$i])); }
else {
	$EndDate = 'None'; }
$pdf->CreateTextBox($EndDate, 15, $currY, 15, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemProductName[$i], 30, $currY, 79, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemName[$i], 109, $currY, 25, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemImpressions[$i], 134, $currY, 15, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemClicks[$i], 149, $currY, 7, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemCostImpressions[$i], 156, $currY, 8, 6, 5, '', 'C');
$pdf->CreateTextBox($LineItemCostClicks[$i], 164, $currY, 8, 6, 5, '', 'C');
$pdf->CreateTextBox(money_format('%(#0n', $AgreedPrice[$i]), 172, $currY, 15, 6, 5, '', 'R');
$currY=$currY+6;
$pdf->line(12,$currY,199,$currY);


	$total = $total+$AgreedPrice[$i];
	$UnRoundedTotal = $UnRoundedTotal+$UnRoundedAgreedPrice[$i];

	if($debug){ echo "Line Item #$i: ".$LineItemProductName[$i]." - ".$AgreedPrice[$i]."   Rounded Total: ".$total."<br>"; }
	if($debug){ echo "Line Item #$i: ".$LineItemProductName[$i]." - ".$UnRoundedAgreedPrice[$i]."   Unrounded Total: ".$UnRoundedTotal."<br>"; }
	if($debug){ echo "<t>----------<br>"; }

	$i++;
	if ($currY > 250) {
		$pdf->AddPage();
		$currY = 40;
	}
// *****************************************************
//
// End of the Booking Details section
//
// *****************************************************
}

$agency_commission = $total*$commission;
$total_less_commission = $total-($total*$commission);
$gst_paid = $total*$gst;
$total_value = $total+$gst_paid;
$total_billable = $total_value - $agency_commission;

if($debug){ echo " <b>Billable and Commission Information</b><br>"; }
if($debug){ echo "Total: ".$total."<br>"; }
if($debug){ echo "Commission %: ".$commission."<br>"; }
if($debug){ echo "Agency Commission: ".$agency_commission."<br>"; }
if($debug){ echo "GST: ".$gst."<br>"; }
if($debug){ echo "Total Less Commission: ".$total_less_commission."<br>"; }
if($debug){ echo "Total Value: ".$total_value."<br>"; }
if($debug){ echo "Total Billable: ".$total_billable."<br>"; }
if($debug){ echo "<t>----------<br>"; }


$currY=$currY-2;
// *****************************************************
//
// Create the Sub Total Section of the proposal.
//    Calculate the commission
//    Calculate the GST and display the billable total
//
// *****************************************************
// $textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L'
$pdf->CreateTextArea('MEDIA-SUB-TOTAL (Excluding GST)', 80, $currY, 40, 8, 6,'B','R');
$currY=$currY+2;
$pdf->CreateTextArea('Comments', 2, $currY, 40, 8, 6,'','L');

$currY=$currY+6;
$pdf->CreateShadedTextBox($ProposalNotes, 2, $currY, 125, 24, '', '','');

$currY=$currY-1;
$pdf->CreateTextArea('Campaign Media Sub-Total (excl GST)', 133, $currY, 40, 8, 6,'','R');
$pdf->CreateTextArea(money_format('%(#0n', $total), 168, $currY, 20, 8, 6,'','R');
$pdf->CreateTextArea($gst_num.'% GST', 133, $currY+4, 40, 8, 6,'','R');
$pdf->CreateTextArea(money_format('%(#0n', $gst_paid), 168, $currY+4, 20, 8, 6,'','R');
$pdf->CreateTextArea('TOTAL MEDIA INVOICE VALUE (Inc GST)', 133, $currY+8, 40, 8, 6,'B','R');
$pdf->CreateTextArea(money_format('%(#0n', $total_value), 168, $currY+8, 20, 8, 6,'','R');
$currY=$currY+3;
$pdf->CreateTextArea('Less Agency Rebate (Inc GST)', 133, $currY+12, 40, 8, 6,'','R');
$pdf->CreateTextArea(money_format('%(#0n', $agency_commission), 168, $currY+12, 20, 8, 6,'','R');
$pdf->CreateTextArea('Net Amount Payable', 133, $currY+16, 40, 8, 6,'','R');
$pdf->CreateTextArea(money_format('%(#0n', $total_billable), 168, $currY+16, 20, 8, 6,'','R');
// *****************************************************
//
// End of the Sub Total section
//
// *****************************************************
$currY=$currY+24;
if ($currY > 250) {
	$pdf->AddPage();
	$currY = 40;
}
//------------
$pdf->CreateHeaderTextBox('BILLING DETAILS', 2, $currY, 186, 3, 6, 'B', 'C');
//------------
// Header for columns
//
$currY=$currY+4;
// $textval, $x = 0, $y, $width = 0, $height = 10, $fontsize = 10, $fontstyle = '', $align = 'L'
$pdf->CreateTextArea('Media', 0, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 40, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 65, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 85, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 100, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('Agency Applicable Only', 117, $currY, 25, 8, 5, 'B','C');
$pdf->CreateTextArea('', 130, $currY, 15, 8, 5, '','C');
$currY=$currY+1.5;
$pdf->CreateTextArea('', 40, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 65, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 85, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 100, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('Less Agency', 115, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('', 130, $currY, 15, 8, 5, '','C');
$currY=$currY+1.5;
$pdf->CreateTextArea('', 40, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 65, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('', 85, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea('Total Invoice', 100, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('Rebate', 115, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('Net Amount', 130, $currY, 15, 8, 5, 'B','C');
$currY=$currY+1.5;
$pdf->CreateTextArea('Invoice Period', 40, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('Campaign Sub-Total (excl GST)', 65, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('15% GST', 85, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('(inc GST)', 100, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('(inc GST)', 115, $currY, 15, 8, 5, 'B','C');
$pdf->CreateTextArea('Payable', 130, $currY, 15, 8, 5, 'B','C');
// End of Column Headers
// -----------------
$i=0;
$TotalA=0;
$TotalB=0;
$TotalC=0;
$TotalD=0;
$TotalE=0;
while ($i < $number_of_lines){
$currY=$currY+2;
	$pdf->CreateTextArea($BillableMonth[$i], 40, $currY, 15, 8, 5, '','C');
	// Add up all the 'Campaign Sub-Total (excl GST) columns
	$A[$i] = $BillableMonthAmount[$i];
	$TotalA = $TotalA+$A[$i];
	$pdf->CreateTextArea(money_format('%(#0n', $A[$i]), 65, $currY, 15, 8, 5, '','C');
	// Add up all the '15% GST' Columns
	$B[$i] = $BillableMonthAmount[$i]*$gst;
	$TotalB = $TotalB+$B[$i];
	$pdf->CreateTextArea(money_format('%(#0n', $B[$i]), 85, $currY, 15, 8, 5, '','C');
	// Add up all the 'Total Invoice (inc GST)' columns
	$C[$i] = $BillableMonthAmount[$i]+($BillableMonthAmount[$i]*$gst);
	$TotalC = $TotalC+$C[$i];
	$pdf->CreateTextArea(money_format('%(#0n', $C[$i]), 100, $currY, 15, 8, 5, '','C');
	// Add up all the 'Less Agency Rebate (inc GST)' columns
	$D[$i] = ($BillableMonthAmount[$i])*$commission;
	$TotalD = $TotalD+$D[$i];
	$pdf->CreateTextArea(money_format('%(#0n', $D[$i]), 115, $currY, 15, 8, 5, '','C');
	// Add up all the 'Net Amount Payable' columns
	// Net Amount Payable = Subtotal +            15 GST             -                Agency Rebate                                
	$E[$i] = $BillableMonthAmount[$i]+($BillableMonthAmount[$i]*$gst)-(($BillableMonthAmount[$i])*$commission);
	$TotalE = $TotalE+$E[$i];
	$pdf->CreateTextArea(money_format('%(#0n', $E[$i]), 130, $currY, 15, 8, 5, '','C');
	$i++;
}
$currY=$currY+5;
$pdf->CreateTextArea('Total:', 40, $currY, 15, 8, 5, '','R');
$pdf->CreateTextArea(money_format('%(#0n', $TotalA), 65, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea(money_format('%(#0n', $TotalB), 85, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea(money_format('%(#0n', $TotalC), 100, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea(money_format('%(#0n', $TotalD), 115, $currY, 15, 8, 5, '','C');
$pdf->CreateTextArea(money_format('%(#0n', $TotalE), 130, $currY, 15, 8, 5, '','C');
$currY=$currY+6;
$pdf->CreateTotalTextBox('', 65, $currY, 48, 1, '', '','');
$pdf->CreateTotalTextBox('', 118, $currY, 25, 1, '', '','');

$currY=$currY+10;

$t_c_line1 = "Please email all creative (ads) to traffic@apnonline.co.nz along with this booking conformation.";
$t_c_line2 = "Standard creative is due at least 3 working days before the start date, rich media is due at least 5 days before the start date.";
$t_c_line3 = "All creative must adhere to our technical specifications and flash instructions. Failure to do so will result in delays and poor ad performance.";
$t_c_line4 = "The rate card, technical specifications, flash Instructions and terms and conditions of advertising can all be found on our website www.advertise.nzherald.co.nz";

// End of the proposal page - now create a new page for all the Terms and Conditions.
// *****************************************************
//
// Create the Terms and Conditions Section of the proposal.
//
// *****************************************************
$pdf->CreateHeaderTextBox('Terms and Conditions', 2, $currY, 186, 3, 6, 'B', 'C');
$currY=$currY+2;
//$pdf->CreateTextArea('Terms and Conditions:', 0, $currY, 40, 8, 10,'','L');
$pdf->CreateTextArea($t_c_line1, 2, $currY, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line2, 2, $currY+3, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line3, 2, $currY+6, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line4, 2, $currY+9, 185, 8, 6,'','L');
/*
$pdf->CreateTextArea($t_c_line4a, 0, $currY+23, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line4b, 0, $currY+26, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line5a, 0, $currY+31, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line5b, 0, $currY+34, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line6a, 0, $currY+39, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line6b, 0, $currY+42, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line7a, 0, $currY+47, 185, 8, 6,'','L');
$pdf->CreateTextArea($t_c_line7b, 0, $currY+50, 185, 8, 6,'','L'); */

// *****************************************************
//
// End of the Terms and Conditions Section of the proposal.
//
// *****************************************************
  
//Close and output PDF document
$pdf->Output('AgencyProposal', 'I');

?>